//
//  YKDragerViewController.m
//  抽屉效果
//
//  Created by yk on 16/3/19.
//  Copyright © 2016年 yk. All rights reserved.
//

#import "YKDragerViewController.h"
#import "YKNavigationController.h"
#import "YKMainViewController.h"
@interface YKDragerViewController ()
@property (weak, nonatomic)  UIView *mainView;
@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (strong,nonatomic) UIBarButtonItem *barBtn;
@property (weak, nonatomic)  UIView *NavView;
@property (nonatomic,strong)NSMutableArray *btns;
@end

@implementation YKDragerViewController

-(NSMutableArray *)btns{
    
    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    YKMainViewController *mainVC = [[YKMainViewController alloc]init];
    
    YKNavigationController *nav = [[YKNavigationController alloc]initWithRootViewController:mainVC];
    
    _barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"bar_1"] style:0 target:self action:@selector(clickBtn)];
    
    mainVC.navigationItem.leftBarButtonItem = _barBtn;
    
    _NavView = nav.view;
    
    _mainView = mainVC.view;
    
    [self setbtn];
    
    [self addChildViewController:nav];
    [self.view addSubview:nav.view];

    
   
    //添加点按手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [nav.view addGestureRecognizer:tap];
}

//添加按钮
-(void)setbtn{
    
    CGFloat y = screenH *0.15;
    
    for (int i = 0; i<3; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"main_c%d",i + 1]] forState:UIControlStateNormal];
        
        btn.frame = CGRectMake(0.2 * screenW, y + i * (y + screenH *0.1) , screenW *0.8, 100);
        
        [_mainView addSubview:btn];
        
        [self.btns addObject:btn];
        
    }
}


-(void)clickBtn{
    
    _barBtn.enabled = NO;
    
    CGFloat y = screenH *0.15;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGFloat offsetX = 275.0;
        
        _NavView.frame = [self frameWithOffsetX:offsetX];
        
        _NavView.layer.shadowColor = [UIColor grayColor].CGColor;
        
        _NavView.layer.shadowOffset = CGSizeMake(-5, 5);
        
        _NavView.layer.shadowOpacity = 1.0;
        
       _NavView.layer.shadowRadius = 5;
        
        for (int i =0; i<3; i++) {
            UIButton *btn = self.btns[i];
            
            btn.frame = CGRectMake(0.1 * _NavView.bounds.size.width, y + i * (y + _NavView.bounds.size.height *0.1) , _NavView.bounds.size.width *0.8, _NavView.bounds.size.height *0.15 );
        }
        
    }];
    

}

//点按手势
-(void)tap
{
    _barBtn.enabled = YES;
    
     CGFloat y = screenH *0.15;
    
    [UIView animateWithDuration:0.5 animations:^{
        _NavView.frame = self.view.bounds;
        
        for (int i =0; i<3; i++) {
            UIButton *btn = self.btns[i];
            
            btn.frame = CGRectMake(0.2 * screenW, y + i * (y + screenH *0.1) , screenW *0.8, 100);
        }
        
    }];
}

#define targetRight 275


//修改View的frame
#define maxH 100
-(CGRect)frameWithOffsetX:(CGFloat)offsetX
{
    CGRect frame = _NavView.frame;
    frame.origin.x += offsetX;
    
    NSLog(@"frame.origin.x=====%f",frame.origin.x);
    
    //此处需要修改y的值
    frame.origin.y = fabs(frame.origin.x * maxH / screenW);
    frame.size.height = screenH - (2 * frame.origin.y);
    
    

    return frame;
}

-(CGRect)btnFrameWithOffsetX:(CGFloat)offsetX
{
    CGRect frame = _NavView.frame;
    
    frame.origin.x += offsetX;
    
    NSLog(@"frame.origin.x=====%f",frame.origin.x);
    
    //此处需要修改y的值
    frame.origin.y = fabs(frame.origin.x * maxH / screenW);
    frame.size.height = screenH - (2 * frame.origin.y);
    
    
    
    return frame;
}



@end
